package com.ljx.memorandumMode;

import lombok.Data;

/**
 *
 * 备忘录模式（白箱）
 *
 * 分析：白箱备忘录模式是破坏封装性的。但是通过程序员自律，同样可以在一定程度上实现模式的大部分用意
 *
 * @作者 LJX
 * @日期 2022/12/27 9:57
 */
public class MemorandumWhiteBoxTest {

    public static void main(String[] args) {
        System.out.println("================白箱===============");
        //创建主角
        GameLead gameLead = new GameLead();
        //初始化属性
        gameLead.init();
        System.out.println("================打BOSS前===============");
        System.out.println(gameLead.toString());
        //存档
        //创建角色状态管理者
        RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
        roleStateCaretaker.setRoleStateMemento(gameLead.saveState());
        System.out.println("===================BOSS战====================");
        gameLead.challengeBOSS();
        System.out.println(gameLead.toString());
        System.out.println("===================回档====================");
        gameLead.retreated(roleStateCaretaker.getRoleStateMemento());
        System.out.println(gameLead.toString());
    }

}

/**
 * 游戏主角（发起人角色）
 */
@Data
class GameLead{

    Integer HP;
    Integer ATK;
    Integer DEF;

    /**
     * 初始化主角属性
     */
    public void init(){
        HP = 100;
        ATK = 100;
        DEF = 100;
    }

    /**
     * 挑战BOSS（失败） 数值清零
     */
    public void challengeBOSS(){
        HP = 0;
        ATK = 0;
        DEF = 0;
    }

    /**
     * 存档
     * @return
     */
    public RoleStateMemento saveState(){
        return new RoleStateMemento(HP, ATK, DEF);
    }

    /**
     * 回档
     */
    public void retreated(RoleStateMemento roleStateMemento){
        HP = roleStateMemento.HP;
        ATK = roleStateMemento.ATK;
        DEF = roleStateMemento.DEF;
    }

    @Override
    public String toString() {
        return "GameLead{" +
                "HP=" + HP +
                ", ATK=" + ATK +
                ", DEF=" + DEF +
                '}';
    }
}

/**
 * 游戏状态存储类(备忘录类)
 */
@Data
class RoleStateMemento{
    Integer HP;
    Integer ATK;
    Integer DEF;

    public RoleStateMemento(Integer HP, Integer ATK, Integer DEF) {
        this.HP = HP;
        this.ATK = ATK;
        this.DEF = DEF;
    }

}

/**
 * 角色状态管理者类
 */
@Data
class RoleStateCaretaker{
    private RoleStateMemento roleStateMemento;
}
